package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeCountVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLikeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserLikeManager {


    @Reference
    private UserLikeService userLikeService;

    @Reference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private RecommendUserService recommendUserService;

    /**
     * 保存用户的喜欢
     * @param userLikeId
     */
    public Map saveUserLike(Long userLikeId) {
        //1.获取当前用户id
        Long loginUserId = UserHolder.getId();
        //2.保存用户的喜欢
        userLikeService.saveUserLike(loginUserId, userLikeId);
        //3.调用方法判断法用户是否互相喜欢
        boolean flag = userLikeService.isUserLike(loginUserId, userLikeId);
        if (flag) {
            //4.true    相互喜欢
            //4.1 加friend好友关系
            friendService.saveContact(loginUserId, userLikeId);
            //4.2加环信好友关系
            huanXinTemplate.addContacts(loginUserId, userLikeId);
        }

        return null;

    }

    /**
     *  用户不喜欢
     * @param userDislikeId
     */
    public void deleteUserLike(Long userDislikeId) {
        //1.获取用户登录id
        Long loginUserId = UserHolder.getId();
        //2.删除用户的喜欢
        userLikeService.deleteUserLike(loginUserId, userDislikeId);
        //3.删除环信好友
        huanXinTemplate.deleteContacts(loginUserId, userDislikeId);
        //4.删除MongoDB中的好友关系
        friendService.delete(loginUserId, userDislikeId);
    }

    /**
     * 查看我的喜欢   粉丝  互相喜欢数量
     * @return
     */
    public UserLikeCountVo userCounts() {
        Long loginUserId = UserHolder.getId();
        //1.查询互相喜欢
        Long eachLoveCount = userLikeService.findEachLoveCount(loginUserId);
        //2.查询我的喜欢
        Long loveCount = userLikeService.findLoveCount(loginUserId);
        //3.查询粉丝
        Long fanCount = userLikeService.findFanCount(loginUserId);
        //4.封装UserLikeCountVo   对象
        UserLikeCountVo vo = new UserLikeCountVo();
        vo.setEachLoveCount(eachLoveCount);//互相喜欢
        vo.setLoveCount(loveCount);//我的喜欢
        vo.setFanCount(fanCount);//粉丝
        return vo;
    }

    /**
     * 查看   互相喜欢、喜欢、粉丝、历史访客 - 翻页列表
     *  1.互相喜欢
     *  2.我关注
     *  3.粉丝
     *  4.历时访客
     */
    public PageBeanVo findUserList(Integer pageNumber, Integer pageSize, Integer type) {
        //1.获得而当前用户的id
        Long loginUserId = UserHolder.getId();
        //2.根据当前用户的id和当前查询用户的类型 调用方法
        PageBeanVo pageBeanVo = userLikeService.findUserList(pageNumber, pageSize, type, loginUserId);
        //UserLikeVo    此实体类需要返回id即可
        List<UserLikeVo> items = (List<UserLikeVo>) pageBeanVo.getItems();
        //3.查询用户的基本信息
        if (CollectionUtil.isNotEmpty(items)) {
            for (UserLikeVo userLikeVo : items) {
                //查询用户记录
                UserInfo userInfo = userInfoService.findById(userLikeVo.getId());
                //封装数据 -->封装基本数据 缺缘分值
                BeanUtil.copyProperties(userInfo, userLikeVo);
                //我和返回的用户之间的缘分值
                RecommendUser recommendUser = recommendUserService.findRecommendUser(loginUserId, userLikeVo.getId());
                if (recommendUser == null) {
                    userLikeVo.setMatchRate(RandomUtil.randomInt(60, 90));//如果没有缘分值，给默认值
                } else {
                    userLikeVo.setMatchRate(recommendUser.getScore().intValue());//有缘分值 直接获取
                }
            }
        }
        return pageBeanVo;
    }

    /**
     * 粉丝---取消喜欢
     * @param uid
     */
    public void deleteLike(Long uid) {
        //1.获取当前登录用户的id
        Long loginUserId = UserHolder.getId();
        //2.取消我对她的喜欢
        userLikeService.deleteLike(loginUserId,uid);
        //3.删除环信好友关系
        huanXinTemplate.deleteContacts(loginUserId, uid);
        //4.删除MongoDB的friend好友关系
        friendService.delete(loginUserId, uid);
    }
}
